package com.ht.api.call.func.impl.transfer;

import java.util.ArrayList;
import java.util.List;

import com.ht.api.bean.AjaxResult;
import com.ht.api.bean.LoginBean;
import com.ht.api.call.func.IFunctionCall;
import com.ht.api.db.Db;
import com.ht.api.db.Entity;
import com.ht.api.db.exception.HtException;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

/**
 * 新增客户接替信息
 * @author asq
 * @createTime 2025年3月9日 09:11:00
 */
public class TransferAddFunctionCall implements IFunctionCall {
	
	@Override
	public String getName() {
		return "新增客户接替信息";
	}

	@Override
	public JSONObject execute(LoginBean me, JSONObject param, AjaxResult result) {
		// 验证参数
		checkParam(param);
		long appId = param.getLong("appid");
		List<Entity> list = new ArrayList<>();
		
		// 客服接替表
		Entity wxTransferUser = Db.createEntity("wx_transfer_user");
		wxTransferUser.set("appid", appId);
		wxTransferUser.set("handover_userid", param.getStr("handover_userid"));
		wxTransferUser.set("takeover_userid", param.getStr("takeover_userid"));
		wxTransferUser.set("transfer_success_msg", param.getStr("transfer_success_msg"));
		wxTransferUser.set("type", param.getInt("type"));
		wxTransferUser.set("remark", param.getStr("remark"));
		wxTransferUser.set("transfer_time", param.getStr("transfer_time"));
		wxTransferUser.set("status", 0);
		list.add(wxTransferUser);
		
		// 客服接替明细表
		JSONArray customerArr = param.getJSONArray("external_userid");
		for (int i = 0; i < customerArr.size(); i++) {
			Entity wxTransferUserItem = Db.createEntity("wx_transfer_user_item");
			wxTransferUserItem.set("appid", appId);
			wxTransferUserItem.set("transfer_id", wxTransferUser.getMainId());
			wxTransferUserItem.set("handover_userid", param.getStr("handover_userid"));
			wxTransferUserItem.set("takeover_userid", param.getStr("takeover_userid"));
			wxTransferUserItem.set("external_userid", customerArr.get(i));
			wxTransferUserItem.set("status", 0);
			list.add(wxTransferUserItem);
		}
		
		// 批量保存
		Db.insertBatchWithStream(me.getStreamLoadConfig(), list);
		
		return JSONUtil.createObj();
	}

	/**
	 * 验证参数合法性
	 * @param param
	 */
	private void checkParam(JSONObject param) {
		// 验证appid
		if (StrUtil.isBlank(param.getStr("appid"))) {
			throw new HtException("appid字段不能为空~");
		}
		
		// 验证微信接口所需参数
		if (StrUtil.isBlank(param.getStr("handover_userid"))) {
			throw new HtException("handover_userid不能为空~");
		}
		if (StrUtil.isBlank(param.getStr("takeover_userid"))) {
			throw new HtException("takeover_userid不能为空~");
		}
		JSONArray externalUseridArr = param.getJSONArray("external_userid");
		if (externalUseridArr == null || externalUseridArr.isEmpty()) {
			throw new HtException("external_userid不能为空~");
		}
		
		// 验证其它参数
		if (StrUtil.isBlank(param.getStr("type"))) {
			throw new HtException("type不能为空~");
		}
		if (StrUtil.isBlank(param.getStr("transfer_time"))) {
			throw new HtException("transfer_time不能为空~");
		}
	}
}
